package com.lanou.musicproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.AlbumContentACtivity;
import com.lanou.musicproject.bean.Album;
import com.lanou.musicproject.bean.SingerAlbum;
import com.lanou.musicproject.utils.MyConfig;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class SeekSingerAlbumGridView extends BaseAdapter {
    private List<SingerAlbum.DataEntity>data;
    private Context mContext;
    public SeekSingerAlbumGridView (Context c, List<SingerAlbum.DataEntity>d){
        mContext = c ;
        data = d;
    }
    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ?data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.seeksingeralbum_item,null);
            holder = new ViewHolder();
            holder.singeralbum_image = (ImageView) convertView.findViewById(R.id.singeralbum_image);
            holder.singeralbum_name = (TextView) convertView.findViewById(R.id.singeralbum_name);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        SingerAlbum.DataEntity entity = (SingerAlbum.DataEntity) getItem(position);
        if (entity !=null){
            holder.singeralbum_name.setText(entity.getName());
            String image = entity.getPicUrl();
            Picasso.with(mContext).load(image).into(holder.singeralbum_image);
            holder.singeralbum_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String album = data.get(position).getAlbumId()+"";
                    Intent intent =new Intent(mContext, AlbumContentACtivity.class);
                    String image = data.get(position).getPicUrl();
                    intent.putExtra("url", image);
                    MyConfig.putSharePre(mContext, "album", "albumid", album);
                    mContext.startActivity(intent);

                }
            });

        }


        return convertView;
    }
   private  class ViewHolder{
       private ImageView singeralbum_image;
       private TextView singeralbum_name;

   }
}
